<?php
 class AccountController extends Zend_Controller_Action
 { 
 		
 	public function init()
 	{ 		 
 		 
 	}
 	
 	public function loginAction()
 	{
 		if(isset($_POST['login']))
 		{
	 		 $username = $this->_request->getPost('username');
	 		 $password = $this->_request->getPost('password');
	 		 $validate = new Application_Model_Authenticate();
	 		 $error = $validate->authLogin($username, $password);
	 		 if($error != "")
	 		 {
	 		 	$this->view->error = $error;
	 		 }
	 		 else {		 		 
		 		 $sql = "select * from customer where customer_name='$username' and customer_password='$password'";    	
		         $connect = new Application_Model_ConnectDB();
		         $data = $connect->dbTable($sql);
		    	 $user_id = $data[0]['customer_id'];
		    	 $order_id = "IP" .substr($user_id,-3);    	  	  	
		    	 $auth = Zend_Auth::getInstance();
		    	 $auth->getStorage()->write(array('user_id' => $user_id,'order_id' => $order_id));
		    	 $this->_redirect('/index/index');
	 		 }
	 		 
 		}    	  			
 	}
 	
 	public function logoutAction()
 	{
 		$auth = Zend_Auth::getInstance();
 		$auth->clearIdentity();
 		$this->_redirect('index/index');
 	}

 	public function registerAction()
 	{ 		
 		if(isset($_POST['register']))
 		{ 		
	 		$username = $this->_request->getParam('username');
	 		$pasword = $this->_request->getParam('password');
	 		$confPass = $this->_request->getParam('confPass');
	 		$email = $this->_request->getParam('email');
	 		$confEmail = $this->_request->getParam('confEmail');
	 		$captcha = $this->_request->getParam('captcha');		
	 		$auth = new Application_Model_Authenticate();
	 		$error = $auth->authAccount($username,$pasword,$confPass,$email,$confEmail,$captcha);
	 		if($error != "")		
	 	 		$this->view->data = $error;
	 	 	else 
	 	 	{
	 	 		$connect = new Application_Model_ConnectDB();	 	 		
	 	 		$sql = "select * from customer";
	 	 		$dbTable = $connect->dbTable($sql);
	 	 		$numRows = count($dbTable);
	 	 		$regdate = date('Y-m-d H:m:s');
	 	 		if(!$numRows)
	 	 		{
	 	 			$customer_id = "KH00" .($numRows + 1);
	 	 			$profile = array('customer_id' => $customer_id,
	 	 							 'customer_name' => $username,
	 	 							 'customer_password' => $pasword,
	 	 							 'customer_regdate' => $regdate,
	 	 							 'customer_last_login' => $regdate);
	 	 			$connect->dbConnect()->insert('customer', $profile);
	 	 		}	 	 		
	 	 		else {	 	 			 	 				
	 	 			while(true)
	 	 			{	 	 				
	 	 				$numRows = $numRows + 1;	 	 				 		
	 	 				if($numRows > 0 && $numRows <= 9)
	 	 					$customer_id = "KH00".$numRows;
	 	 				else if($numRows > 9 && $numRows <= 99)
	 	 					$customer_id = "KH0".$numRows;	 	 				
	 	 				else 
	 	 					$customer_id = "KH".$numRows;
	 	 				$query = "select * from customer where customer_id = '$customer_id'";
	 	 				$res = $connect->dbTable($query);	 	 							
	 	 				if(!count($res))	 	 				
	 	 				{
	 	 					$profiles = array('customer_id' => $customer_id,
	 	 									 'customer_name' => $username,
	 	 									 'customer_password' => $pasword,
	 	 									 'customer_regdate' => $regdate,
	 	 									 'customer_last_login' => $regdate);
	 	 					$connect->dbConnect()->insert('customer', $profiles);
	 	 					$this->_redirect('/index/index');	 	 					
	 	 				}
	 	 				else
	 	 					continue; 		 	 					
	 	 			}	 	 				
	 	 		} 	 	 			 	 		
	 	 	}
 		}
 		else 
 			$this->view->data = '';	 		 		
 	} 	
 }